const baseURL = "http://121.89.205.189/api"
// https://developers.weixin.qq.com/miniprogram/dev/api/network/request/wx.request.html

const token = wx.getStorageSync('token') || ''
const request = {
  get (url, data) {
    // 显示加载动画
    // https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showLoading.html
    wx.showLoading({
      title: '正在加载中...'
    })
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseURL + url,
        data: data || {},
        method: 'GET',
        header: { token },
        success (res) {
          if (res.data.code === '10119') {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          } else {
            resolve(res)
          }
        },
        fail () {
          reject()
        },
        complete () {
          // 取消加载动画
          wx.hideLoading()
        }
      })
    })
   
  },
  post (url, data) {
    // 显示加载动画
    // https://developers.weixin.qq.com/miniprogram/dev/api/ui/interaction/wx.showLoading.html
    wx.showLoading({
      title: '正在加载中...'
    })
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseURL + url,
        data: data || {},
        method: 'POST',
        header: { token },
        success (res) {
          if (res.data.code === '10119') {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          } else {
            resolve(res)
          }
        },
        fail () {
          reject()
        },
        complete () {
          // 取消加载动画
          wx.hideLoading()
        }
      })
    })
   
  }
}

export default request